﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CutShuFa.AutoAction
{
    public partial class AutoActionParameterControl : UserControl
    {
        IAutoAction action;
        public event EventHandler? RunClick;
        public event EventHandler? DelClick;
        public event EventHandler? UpClick;
        public event EventHandler? DownClick;
        public AutoActionParameterControl()
        {
            InitializeComponent();
        }

        public void Init(IAutoAction action)
        {
            this.action = action;
            lb_name.Text = action.ActionName;
            List<Parameter> parameters = action.Parameters;
            panel.Controls.Clear();
            foreach (var parameter in parameters)
            {
                switch (parameter.Type)
                {
                    case ParameterType.Int:
                        var intParameterControl = new IntParameterControl();
                        intParameterControl.Init(parameter);
                        panel.Controls.Add(intParameterControl);
                        break;
                    case ParameterType.Double:
                        var doubleParameterControl = new DoubleParameterControl();
                        doubleParameterControl.Init(parameter);
                        panel.Controls.Add(doubleParameterControl);
                        break;
                    case ParameterType.Color:
                        var colorParameterControl = new ColorParameterControl();
                        colorParameterControl.Init(parameter);
                        panel.Controls.Add(colorParameterControl);
                        break;
                    case ParameterType.Mat:
                        var matParameterControl = new MatParameterControl();
                        matParameterControl.Init(parameter);
                        panel.Controls.Add(matParameterControl);
                        break;
                }
            }
        }

        public List<Parameter> GetParameters()
        {
            var parameters = new List<Parameter>();
            foreach (var control in panel.Controls)
            {
                var parameterControl = control as IParameterControl;
                if (parameterControl != null)
                {
                    var parameter = parameterControl.GetParameter();
                    if (parameter != null)
                    {
                        parameters.Add(parameter);
                    }
                }
            }
            return parameters;
        }

        private void bt_run_Click(object sender, EventArgs e)
        {
            if(RunClick != null)
            {
                RunClick(this, e);
            }
        }

        private void bt_del_Click(object sender, EventArgs e)
        {
            if(DelClick != null)
            {
                DelClick(this, e);
            }
        }

        private void bt_up_Click(object sender, EventArgs e)
        {
            if(UpClick != null) { UpClick(this, e); }
        }

        private void bt_down_Click(object sender, EventArgs e)
        {
            if(DownClick != null) { DownClick(this, e);}
        }

        public IAutoAction GetAction()
        {
            action.Parameters = GetParameters();
            return action;
        }
    }
}
